const express = require('express');
const fs = require('fs');
const app = express();
// app.use(express.static('public'));
// 自定义中间件函数
function myStatic (dir) {
    return (req, res, next) => {
        let path = req.path;
        if (path === '/') {
            path = '/index.html'
        }
        let filePath = __dirname + '/' + dir + path;
        fs.readFile(filePath, (error, data) => {
            if(error) {
                next()
            }else {
                // res.end(data);
                console.log(data.toString());
                res.send(data.toString());
            }
        })
    }
}
app.use(myStatic('public'));

app.get('/middleware',(req, res) => {
    let data = '王俊凯';
    res.send(data)
    
}) 

app.listen('8989', () => {
    console.log('服务已启动，http://localhost:8989');
})